from django.core.management.base import BaseCommand, CommandError
from django.conf import  settings


import time
import pika


'''
rabbitmq 接收者
'''

class Command(BaseCommand):

    def __init__(self):
        host = settings.RABBITMQ['HOST']
        port = settings.RABBITMQ['PORT']
        connection = pika.BlockingConnection(pika.ConnectionParameters(host=host,port='5672'))

        channel = connection.channel()

        self.connection = connection
        self.channel = channel




    def handle(self, *args, **options):

        def callback(ch, method, properties, body):
            time.sleep(2)
            print(" [x] Received %r" % body)

        self.channel.basic_consume(queue='hello',
                              auto_ack=True,
                              on_message_callback=callback)
        self.channel.start_consuming()